#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author:taotieren


"""
    rpi.gpio 需要 root 权限，板上运行需要使用
    sudo python gui_slider_RGB.py
    RGB LED 串 470Ω 电阻共阴极连接
"""
from tkinter import *

""" python3 中请安装 tkintertoy 来进行程序调试和验证 """
import RPi.GPIO as GPIO

""" 非树莓派环境下可以用 rpi.gpio_def 代替 rpi.gpio 来进行程序调试和验证 """
import time

GPIO.setmode(GPIO.BCM)
GPIO.setup(18, GPIO.OUT)
GPIO.setup(23, GPIO.OUT)
GPIO.setup(24, GPIO.OUT)

pwmRed = GPIO.PWM(18, 500)
pwmRed.start(100)

pwmGreen = GPIO.PWM(23, 500)
pwmGreen.start(100)

pwmBule = GPIO.PWM(24, 500)
pwmBule.start(100)


class App:

    def __init__(self, master):
        frame = Frame(master)
        frame.pack()
        Label(frame, text='Red').grid(row=0, column=0)
        Label(frame, text='Green').grid(row=1, column=0)
        Label(frame, text='Bule').grid(row=2, column=0)
        scaleRed = Scale(frame, from_=0, to=100,
                         orient=HORIZONTAL,
                         command=self.updateRed)
        scaleRed.grid(row=0, column=1)
        scaleGreen = Scale(frame, from_=0, to=100,
                           orient=HORIZONTAL,
                           command=self.updateGreen)
        scaleGreen.grid(row=1, column=1)
        scaleBule = Scale(frame, from_=0, to=100,
                          orient=HORIZONTAL,
                          command=self.updateBule)
        scaleBule.grid(row=2, column=1)

    def updateRed(self, duty):
        pwmRed.ChangeDutyCycle(float(duty))

    def updateGreen(self, duty):
        pwmGreen.ChangeDutyCycle(float(duty))

    def updateBule(self, duty):
        pwmBule.ChangeDutyCycle(float(duty))


root = Tk()
root.wm_title('RGB LED Control')
app = App(root)
root.geometry("200x150+0+0")
root.mainloop()
